import sys
import os
import argparse
print(sys.argv)
# print(os.listdir('../'))

#
# def listdir(path, with_dir=False):
#     result = []
#     for f in os.listdir(path):
#         if with_dir or os.path.isfile(os.path.join(path, f)):
#             result.append(f)
#         return result
#
# # 通过获取参数来展示输入目录下的文件列表，且在-all时同时输出文件夹
# # if __name__ == '__main__':
# #     if sys.argv[1] == '--dir':
# #         path = sys.argv[2]
# #         with_dir = len(sys.argv) > 3 and sys.argv[3] == 'all'
# #         print(listdir(path, with_dir))
#
#
# if __name__ == '__main__':
#     parser = argparse.ArgumentParser()                      # 开启参数解析
#     parser.add_argument('--dir', default='.')               # 必填位置参数
#     parser.add_argument('all', nargs='?', default=False)    # 可选参数，默认为False
#     args = parser.parse_args()
#     print(listdir(args.dir, args.all))


# 遍历文件夹

def traversal_files(path):
    filenum = 0
    filesize = 0
    for root, dirs, files in os.walk(path):
        for f in files:
            filename = os.path.join(root, f)
            size = os.path.getsize(filename)
            print('文件名为{}的文件，大小为：{}\t'.format(f, size))
            if os.path.isfile(filename):
                filenum += 1
                filesize += size
    print('{}文件路径下的文件大小为：{}'.format(path, filesize))


if __name__ == '__main__':
    traversal_files(sys.argv[2])